package com.zjitc.utils;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtil {
  public static void sendMail(String email, String emailMsg)
      throws AddressException, MessagingException {
    // 1.创建一个程序与邮件服务器会话对象 Session
    //email:目的地账户
    //emailMsg:发送的内容
    Properties props = new Properties();
    //设置发送的协议
    props.setProperty("mail.transport.protocol", "SMTP");

    //设置发送邮件的服务器
    props.setProperty("mail.host", "smtp.sina.com");
    props.setProperty("mail.smtp.auth", "true");// 指定验证为true

    // 创建验证器
    Authenticator auth = new Authenticator() {
      public PasswordAuthentication getPasswordAuthentication() {
        //设置发送人的帐号和密码
        return new PasswordAuthentication("lww0523@sina.com", "lww19980523520");
      }
    };

    Session session = Session.getInstance(props, auth);

    // 2.创建一个Message，它相当于是邮件内容
    Message message = new MimeMessage(session);

    //设置发送者
    message.setFrom(new InternetAddress("lww0523@sina.com"));

    //设置发送方式与接收者
    message.setRecipient(RecipientType.TO, new InternetAddress(email));

    //设置邮件主题
    message.setSubject("用户激活");
    // message.setText("这是一封激活邮件，请<a href='#'>点击</a>");

    //设置邮件内容
    message.setContent(emailMsg, "text/html;charset=utf-8");

    // 3.创建 Transport用于将邮件发送
    Transport.send(message);
  }
    //static Properties properties;
    //static Message msg;
    //static Transport transport;
    ////初始化Mail信息
    //public MailUtil(){
    //  properties = new Properties();
    //
    //  properties.setProperty("mail.debug", "true");//调试模式发送
    //  properties.setProperty("mail.smtp.auth", "true");//身份验证设置
    //  properties.setProperty("mail.host", "smtp.sina.com");//发件人邮箱主机名
    //  properties.setProperty("mail.transport.protocol", "smtp");//发件协议
    //
    //  Session session = Session.getInstance(properties);
    //
    //  msg = new MimeMessage(session);
    //
    //  try {
    //    msg.setSubject("新潮网站验证邮件");
    //    msg.setFrom(new InternetAddress("lww"));//设置发件人
    //
    //    transport = session.getTransport();
    //    transport.connect("lww0523@sina.com", "lww19980523520");//设置发件人在该邮箱主机上的用户名密码
    //  } catch (MessagingException e) {
    //    e.printStackTrace();
    //  }
    //}
    ///**
    // * 得到邮箱地址邮箱内容发送。
    // * @throws AddressException
    // * @throws MessagingException
    // */
    //
    //public void sendMail(String address,String text) throws AddressException, MessagingException{
    //  msg.setText(text);
    //  transport.sendMessage(msg, new Address[] {new InternetAddress(address)});
    //  transport.close();
    //}


  }

